package ema.tpjee.banking.session;

import java.io.Serializable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import ema.tpjee.banking.dao.BankDao;
import ema.tpjee.banking.dao.CityDao;
import ema.tpjee.banking.dao.CustomerDao;
import ema.tpjee.banking.model.Bank;
import ema.tpjee.banking.model.City;
import ema.tpjee.banking.model.Customer;




public class BankingSession implements Serializable{
	

	private static final long serialVersionUID = -5012050375526860760L;
	
	static int instanceCount=0;
	
	
	private CustomerDao customerDao;
	private BankDao bankDao;
	private CityDao cityDao;
	
	
	public CustomerDao getCustomerDao() {
		return customerDao;
	}

	public BankDao getBankDao() {
		return bankDao;
	}

	public CityDao getCityDao() {
		return cityDao;
	}


	
	public void createEjbDao() {
		System.out.println("creating  EjbDao");
		System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		System.setProperty(Context.PROVIDER_URL, "localhost:1099");
		System.setProperty(Context.URL_PKG_PREFIXES,
				"org.jboss.naming:org.jnp.interfaces");
		try {
			bankDao = (BankDao) new InitialContext().lookup("BankDaoImpl/remote");
			customerDao = (CustomerDao) new InitialContext().lookup("CustomerDaoImpl/remote");
			cityDao = (CityDao) new InitialContext().lookup("CityDaoImpl/remote");
	
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	public BankingSession() {
		super();
		System.out.println ("creating session "+instanceCount+1);
		createEjbDao();
		if (instanceCount==0){
		  bankDao.populate();
		  customerDao.populate(bankDao.getList().get(0));	
		  System.out.println ("populated for test");
		}
		instanceCount++;
	}	

	public void display()  {
	

		System.out.println("Liste des communes");

		List<City> cities = cityDao.getList();
		if (cities != null) {
			for (City city : cities) {
				System.out.println(city);
			}
		}
		
		System.out.println("Liste des banques");

		List<Bank> banks = bankDao.getList();
		if (banks != null) {
			for (Bank bank : banks) {
				System.out.println(bank);
			}
		}
		
		System.out.println("Liste des clients");

		List<Customer> customers = customerDao.getList();
		if (customers != null) {
			for (Customer customer : customers) {
				System.out.println(customer);
			}
		}

	}

	
	
	public static void main(String[]args){
		BankingSession bs = new BankingSession();
	
		bs.display();
		
	}

}
